package com.yusysqdy.gulimall.seckill.config;

import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
import com.alibaba.fastjson.JSON;
import com.yusysqdy.common.exception.BizCodeEnume;
import com.yusysqdy.common.utils.R;
import org.springframework.context.annotation.Configuration;

/**
 * @program: gulimail
 * @contact: qindy0116@126.com
 * @author: Mr.DYQin
 * @create: 2021-02-25 15:38
 * @Description: 配置请求被限制以后的处理器
 **/

@Configuration
public class SecKillSentinelConfig {
    public SecKillSentinelConfig() {
        WebCallbackManager.setUrlBlockHandler((request, response, exception) -> {
            R error = R.error(BizCodeEnume.TO_MANY_REQUEST.getCode(), BizCodeEnume.TO_MANY_REQUEST.getMsg());
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json");
            response.getWriter().write(JSON.toJSONString(error));
        });
    }
}
